//  AlertMeViewController.m

#import "AlertMeViewController.h"

@implementation AlertMeViewController

@synthesize userOutput,userInput;

NSString *emailTitle = @"Test";

//displays an alert view when the button is clicked 
- (IBAction)doAlert:(id)sender {
	UIAlertView *alertDialog;
	alertDialog = [[UIAlertView alloc] initWithTitle:@"Alert Button Tapped" message:@"I need your attention, NOW!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alertDialog show];
	[alertDialog release];
	
}

-(void)alertView:(UIAlertView *)alertMeView clickedButtonAtIndex:(NSInteger)buttonIndex{
	// Prints the index of the button
	// NSString *buttonSelected = [[NSString alloc] initWithFormat:@"%d", buttonIndex];
	// Print the title of the button
	NSString *buttonSelected = [alertMeView buttonTitleAtIndex:buttonIndex];
	if([alertMeView.title isEqualToString: emailTitle]){
		userOutput.text = userInput.text;
	}
	else {
	
		userOutput.text = buttonSelected;
	}
}

//multiple buttons 
- (IBAction)doMultiButtonAlert:(id)sender {
	//create the alert
	UIAlertView *alertDialog;
	alertDialog = [[UIAlertView alloc] initWithTitle:@"Alert Button Tapped" message:@"Select an option" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Option A", @"Option B", nil];
	[alertDialog show];
	[alertDialog release];
}

//alert with textfield added as subview
- (IBAction)doAlertInput:(id)sender {
	UIAlertView *alertDialog;
	alertDialog = [[UIAlertView alloc] initWithTitle: emailTitle message:@"Blank - making space here" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
	userInput = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 70.0, 260.0, 25.0)];
	[userInput setBackgroundColor:[UIColor whiteColor]];
	[alertDialog addSubview:userInput];
	[alertDialog show];
	[alertDialog release];	
}

- (IBAction)doActionSheet:(id)sender {
	UIActionSheet *actionSheet;
	actionSheet = [[UIActionSheet alloc] initWithTitle:@"Available actions" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destroy" otherButtonTitles:@"Negotiate", @"Compromise", nil];
	actionSheet.actionSheetStyle = UIBarStyleBlackTranslucent;
	[actionSheet showInView:self.view];
}

//Handle result from Action Sheet
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
	NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:@"Destroy"]) {
        userOutput.text=@"Clicked 'Destroy'";
    }else if ([buttonTitle isEqualToString:@"Negotiate"]){
        userOutput.text=@"Clicked 'Negotiate'";
    }else if ([buttonTitle isEqualToString:@"Compromise"]){
        userOutput.text=@"Clicked 'Compromise'";
    }else{
        userOutput.text=@"Clicked 'Cancel'";
    }
}


- (IBAction)doSound:(id)sender
{
	SystemSoundID soundID;
	NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"moo" ofType:@"wav"];
	AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundFile], &soundID);
	AudioServicesPlaySystemSound(soundID);
	//get the text on the button
	NSString *mooButtonText = [sender titleForState:UIControlStateNormal];
	//put the button text in the label
	userOutput.text	= mooButtonText;
	[soundFile release];
	
}

- (IBAction)doAlertSound:(id)sender
{
	SystemSoundID soundID;
	//get the text on the button
	NSString *alertButtonText = [sender titleForState:UIControlStateNormal];
	//put the button text in the label
	userOutput.text = alertButtonText;
	NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"alertsound"  ofType:@"wav"];
	AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:soundFile], &soundID);
	AudioServicesPlayAlertSound(soundID);
	[soundFile release];
}

- (IBAction)doVibration:(id)sender {
    //get the text on the button
    NSString *vibeButtonText = [sender titleForState:UIControlStateNormal];
    //put the button text in the label
    userOutput.text = vibeButtonText;
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}


/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */

/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
 [super viewDidLoad];
 }
 */

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
	return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {
	//release the outlet
	[userInput release];
    [userOutput release];
	[emailTitle release];
    [super dealloc];
}

@end
